package bookmanage;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CodeSED extends JInternalFrame {
	private JScrollPane scrollPane;
	private JTable table;
	private JComboBox comboBox;
	private JLabel lblNewLabel;
	private JLabel label;
	private JComboBox comboBox_1;
	private JLabel label_1;
	private JTextField big_code;
	private JLabel label_2;
	private JTextField small_code;
	private JButton btnNewButton;
	private JButton button;
	private JButton btnNewButton_1;
	private JLabel label_3;
	private JTextField code_no;

	Connection con;
	DefaultTableModel model = new DefaultTableModel();
	Vector<String> colName = new Vector<String>();
	String c_no,big,small;
	String c1,c2;

	public CodeSED(Connection con) {
		super("코드 조회/수정/삭제",true,true,false,true);
		this.con=con;
		colName.add("코드번호");
		colName.add("대분류");
		colName.add("소분류");
		setResizable(false);
		setBounds(100, 100, 550, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getScrollPane());
		getContentPane().add(getComboBox_1());
		getContentPane().add(getComboBox());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getBig_code());
		getContentPane().add(getLabel_2());
		getContentPane().add(getSmall_code());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getButton_1());
		getContentPane().add(getButton_1_1());
		getContentPane().add(getLabel_3());
		getContentPane().add(getCode_no());

		JTableHeader jh = table.getTableHeader();
		jh.setReorderingAllowed(false);
		selectBig();
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(10, 46, 313, 215);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			model = new DefaultTableModel(colName, 0) {
				// 셀 내용 수정 불가
				public boolean isCellEditable(int rowIndex, int mColIndex) {
					return false;
				}
			};
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent e) {
					int idxrow = table.getSelectedRow();
					c_no=(String)table.getValueAt(idxrow, 0);
					big=(String)table.getValueAt(idxrow, 1);
					small=(String)table.getValueAt(idxrow, 2);
					code_no.setText(c_no);
					big_code.setText(big);
					small_code.setText(small);
					if(small==null){
						big_code.setEditable(true);
						small_code.setEditable(false);
					}else{
						big_code.setEditable(false);
						small_code.setEditable(true);
					}
				}
			});
		}
		return table;
	}
	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox();
			comboBox.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					String code1 = (String)comboBox.getSelectedItem();
					comboBox_1.removeAllItems();
					comboBox_1.addItem("선택");
					try {
						String sql="select small_name from codetbl where big_name=? and small_name is not null";
						PreparedStatement psmt = con.prepareStatement(sql);
						psmt.setString(1, code1);
						ResultSet rs = psmt.executeQuery();
						while(rs.next()){
							comboBox_1.addItem(rs.getString(1));
						}
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			});
			comboBox.addItem("선택");
			comboBox.setBounds(60, 14, 100, 22);
		}
		return comboBox;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("대분류");
			lblNewLabel.setBounds(10, 16, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("소분류");
			label.setBounds(172, 18, 57, 15);
		}
		return label;
	}
	private JComboBox getComboBox_1() {
		if (comboBox_1 == null) {
			comboBox_1 = new JComboBox();
			comboBox_1.addItem("선택");
			comboBox_1.setBounds(223, 15, 100, 22);
		}
		return comboBox_1;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("대분류");
			label_1.setBounds(353, 85, 57, 15);
		}
		return label_1;
	}
	private JTextField getBig_code() {
		if (big_code == null) {
			big_code = new JTextField();
			big_code.setEditable(false);
			big_code.setBounds(406, 80, 116, 24);
			big_code.setColumns(10);
		}
		return big_code;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("소분류");
			label_2.setBounds(353, 115, 57, 15);
		}
		return label_2;
	}
	private JTextField getSmall_code() {
		if (small_code == null) {
			small_code = new JTextField();
			small_code.setEditable(false);
			small_code.setColumns(10);
			small_code.setBounds(406, 110, 116, 24);
		}
		return small_code;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("수정");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String code = code_no.getText().trim();
					String big_c = big_code.getText().trim();
					String small_c = small_code.getText().trim();
					int yn = JOptionPane.showConfirmDialog(null, "정말 수정하시겠습니까?",
							"확인창",JOptionPane.YES_NO_OPTION);
					if(yn==JOptionPane.YES_OPTION){
						try {
							String sql="update codetbl set ";
							PreparedStatement psmt;
							if(small==null){
								sql+="big_name=? where big_name=?";
								psmt=con.prepareStatement(sql);
								psmt.setString(1, big_c);
								psmt.setString(2, big);
								psmt.executeUpdate();
							}else{
								sql+="small_name=? where code_no=?";
								psmt=con.prepareStatement(sql);
								psmt.setString(1, small_c);
								psmt.setString(2, code);
								psmt.executeUpdate();
							}
							
						} catch (Exception e2) {
							e2.printStackTrace();
						}
						JOptionPane.showMessageDialog(null, "code_no "+code+"의 정보가 수정되었습니다!");
						selectBig();
						comboBox.setSelectedItem(c1);
						comboBox_1.setSelectedItem(c2);
						selectCode();
					}
				}
			});
			btnNewButton.setBounds(353, 160, 72, 33);
		}
		return btnNewButton;
	}
	private JButton getButton_1() {
		if (button == null) {
			button = new JButton("삭제");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int yn = JOptionPane.showConfirmDialog(null, "정말 삭제 하시겠습니까?",
							"확인창",JOptionPane.YES_NO_OPTION);
					if(yn == JOptionPane.YES_OPTION){
						String cn = code_no.getText().trim();
						if(cn.equals("")){
							JOptionPane.showMessageDialog(null, "코드 번호를 입력해주세요!");
							return;
						}
						try {
							String sql="delete from codetbl where code_no=?";
							PreparedStatement psmt = con.prepareStatement(sql);
							psmt.setString(1, cn);
							psmt.executeUpdate();
						} catch (Exception e2) {
							e2.printStackTrace();
						}
						JOptionPane.showMessageDialog(null, "삭제 되었습니다!");
						code_no.setText("");
						big_code.setText("");
						small_code.setText("");
						comboBox.setSelectedItem(c1);
						comboBox_1.setSelectedItem(c2);
						selectCode();
					}

				}
			});
			button.setBounds(450, 160, 72, 33);
		}
		return button;
	}
	
	public void selectBig(){
		comboBox.removeAllItems();
		comboBox.addItem("선택");
		try {
			String sql="select big_name from codetbl where small_name is null";
			PreparedStatement psmt = con.prepareStatement(sql);
			ResultSet rs = psmt.executeQuery();
			while(rs.next()){
				comboBox.addItem(rs.getString(1));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void selectCode(){
		c1=(String)comboBox.getSelectedItem();
		c2=(String)comboBox_1.getSelectedItem();
		String code1 = "where big_name='"+(String)comboBox.getSelectedItem()+"'";
		String code2 = " and small_name='"+(String)comboBox_1.getSelectedItem()+"'";
		if(code1.contains("선택")) code1="";
		if(code2.contains("선택")) code2="";
		model.setRowCount(0);
		try {
			String sql = "select * from codetbl "+code1+code2;
			System.out.println(sql);
			PreparedStatement psmt = con.prepareStatement(sql);
			ResultSet rs = psmt.executeQuery();
			while(rs.next()){
				Vector<String> vec = new Vector<String>();
				vec.add(rs.getString(1));
				vec.add(rs.getString(2));
				vec.add(rs.getString(3));
				model.addRow(vec);
			}
			table.setModel(model);
		} catch (Exception e) {
			e.printStackTrace();
		}


	}
	private JButton getButton_1_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("조회");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					selectCode();
				}
			});
			btnNewButton_1.setBounds(335, 14, 97, 23);
		}
		return btnNewButton_1;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("코드번호");
			label_3.setBounds(353, 55, 57, 15);
		}
		return label_3;
	}
	private JTextField getCode_no() {
		if (code_no == null) {
			code_no = new JTextField();
			code_no.setEditable(false);
			code_no.setColumns(10);
			code_no.setBounds(406, 50, 116, 24);
		}
		return code_no;
	}
}
